package com.heu.blood.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import com.heu.blood.match.service.BloodInformationService;
import com.heu.blood.recycle.dao.BloodBagDestroyDao;
import com.heu.blood.recycle.entity.BloodBagDestroyEntity;
import com.heu.blood.recycle.service.BloodBagDestroyService;
import com.heu.blood.security.entity.LoginUser;
import com.heu.blood.storge.service.BloodInStorgeService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("bloodBagDestroyService")
public class BloodBagDestroyServiceImpl extends ServiceImpl<BloodBagDestroyDao, BloodBagDestroyEntity> implements BloodBagDestroyService {

    @Autowired
    private BloodBagDestroyDao bloodBagDestroyDao;
    @Autowired
    private BloodInformationService bloodInformationService;
    @Autowired
    private BloodInStorgeService bloodInStorgeService;


    /**
     * 根据bloodID查找血袋销毁信息
     * @param bloodId
     * @return
     */
    @Override
    public BloodBagDestroyEntity query(String bloodId) {
        LambdaQueryWrapper<BloodBagDestroyEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodBagDestroyEntity::getBloodId,bloodId)
                .eq(BloodBagDestroyEntity::getIfShow,1);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        BloodBagDestroyEntity entity = new BloodBagDestroyEntity();
        if(Strings.isNotBlank((String)params.get("bloodId")))
            entity.setBloodId((String)params.get("bloodId"));
        if(Strings.isNotBlank((String)params.get("bloodBagNumber")))
            entity.setBloodBagNumber((String)params.get("bloodBagNumber"));
        if(Strings.isNotBlank((String) params.get("destroyPeople")))
            entity.setDestroyPeople((String)params.get("destroyPeople"));
        if(Strings.isNotBlank((String) params.get("destroyPeopleNumber")))
            entity.setDestroyPeopleNumber((String)params.get("destroyPeopleNumber"));

        //添加时间范围条件
        String startTime = null;
        String endTime = null;
        if(Strings.isNotBlank((String)params.get("startTime")))
            startTime = (String)params.get("startTime");
        if(Strings.isNotBlank((String)params.get("endTime")))
            endTime = (String)params.get("endTime");

        //条件构造器
        LambdaQueryWrapper<BloodBagDestroyEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(entity.getBloodId()),BloodBagDestroyEntity::getBloodId,entity.getBloodId())
                    .eq(Strings.isNotBlank(entity.getBloodBagNumber()),BloodBagDestroyEntity::getBloodBagNumber,entity.getBloodBagNumber())
                    .eq(Strings.isNotBlank(entity.getDestroyPeople()),BloodBagDestroyEntity::getDestroyPeople,entity.getDestroyPeople())
                    .eq(Strings.isNotBlank(entity.getDestroyPeopleNumber()),BloodBagDestroyEntity::getDestroyPeopleNumber,entity.getDestroyPeopleNumber())
                    .ge(Strings.isNotBlank(startTime),BloodBagDestroyEntity::getDestroyDate,startTime)
                    .le(Strings.isNotBlank(endTime),BloodBagDestroyEntity::getDestroyDate,endTime)
                    .eq(BloodBagDestroyEntity::getIfShow,1);

        IPage<BloodBagDestroyEntity> page = this.page(
                new Query<BloodBagDestroyEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public boolean save(BloodBagDestroyEntity bloodBagDestroyEntity) {

        //获取当前线程认证信息
        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null && authentication.isAuthenticated()){
            //获取当前用户id、name
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            //写入当前用户信息
            bloodBagDestroyEntity.setDestroyPeople(loginUser.getUser().getUserName());
            bloodBagDestroyEntity.setDestroyPeopleNumber(String.valueOf(loginUser.getUser().getUserId()));
        }

        int insert = bloodBagDestroyDao.insert(bloodBagDestroyEntity);

        //更新血液信息表
        boolean informationgUpdate = bloodInformationService.updateBloodStateByBloodId(bloodBagDestroyEntity.getBloodId(), "BLOOD_BAG_DESTRUCTION");
        //更新库存表
        boolean storgeUpdate = bloodInStorgeService.updateInStorgeBloodStatebyBloodId(bloodBagDestroyEntity.getBloodId(),"BLOOD_BAG_DESTRUCTION");

        return informationgUpdate && storgeUpdate && insert == 1;
    }
}